package cn.com.thingo.hyintelligent.util;

import android.app.Application;
import android.content.Context;

public class ErrorTipUtils {


    public static String getErrorTip(int id, int errCode, Context context) {
        StringBuffer errorTip = new StringBuffer();

        if (errCode != 0) {
            int errorId = getErrorId(errCode,context);
            if (errorId != 0) {
                errorTip.append(context.getString(errorId));
            } else {
                errorTip.append(context.getString(id)).append(" (").append(errCode).append(")");
            }
        } else {
            errorTip.append(context.getString(id));
        }
        return errorTip.toString();
    }

    private static int getErrorId(int errorCode,Context context) {
        int errorId = context.getResources().getIdentifier("error_code_" + errorCode, "string", context.getPackageName());
        /*
         * Field fieldError; int errorId = 0; try { fieldError =
         * R.string.class.getDeclaredField("error_code_" + errorCode);
         * fieldError.setAccessible(true); R.string string = new R.string(); try { errorId =
         * fieldError.getInt(string); } catch (IllegalAccessException e) { // TODO Auto-generated
         * catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO
         * Auto-generated catch block e.printStackTrace(); } } catch (NoSuchFieldException e) { //
         * TODO Auto-generated catch block e.printStackTrace(); }
         */

        return errorId;
    }
}
